from pydantic import BaseModel
from typing import List, Optional

class UserLogin(BaseModel):
    """登录请求模型"""
    username: str
    password: str

class LoginResponse(BaseModel):
    """登录响应模型"""
    success: bool
    data: dict
    accessToken: str
    refreshToken: str
    expires: str

class RefreshTokenRequest(BaseModel):
    """刷新令牌请求模型"""
    refresh_token: str

class TokenResponse(BaseModel):
    """令牌响应模型"""
    access_token: str
    refresh_token: str
    token_type: str = "bearer"

class Token(BaseModel):
    access_token: str
    refresh_token: str
    token_type: str = "bearer"

class UserBase(BaseModel):
    username: str


class UserCreate(UserBase):
    password: str
    nickname: str


class User(UserBase):
    id: int
    nickname: str
    avatar: str | None = None
    roles: list[str] = []
    permissions: list[str] = []
    is_active: bool

    class Config:
        # 修改这里
        from_attributes = True


class UserLoginResponse(BaseModel):
    avatar: Optional[str] = None
    username: str
    nickname: str  # 添加 nickname 字段
    roles: List[str] = []
    permissions: List[str] = []
    accessToken: str
    refreshToken: str
    expires: str

class DeviceSerial(BaseModel):
    cpu_ser: str
    emmc_ser: str
    mac_add: str

class DeviceInfo(BaseModel):
    cpu_usage: int
    mem_usage: int
    # emmc_usage: int
    ip_address: str
    brightness: int
    volume: int

class DeviceConfig(BaseModel):
    heartbeat_interval: int

# 客户端消息模型
class ClientActivateMessage(BaseModel):
    dev_id: str
    dev_ser: DeviceSerial
    firmware_version: str
    app_version: str
    request: str = "activate"
    event_time: str

class ClientHeartbeatMessage(BaseModel):
    dev_id: str
    dev_info: DeviceInfo
    type: str = "heartbeat"
    event_time: str

# 服务端消息模型
class ServerActivateResponse(BaseModel):
    dev_id: str
    config: DeviceConfig
    latest_app_version: str
    update: bool
    event_time: str

# 设备状态模型
class DeviceStatus(BaseModel):
    id: int
    dev_id: str
    cpu_ser: str
    emmc_ser: str
    mac_add: str
    activated_time: str
    cpu_usage: int
    mem_usage: int
    # emmc_usage: int
    # ip_address: str
    firmware_version: str
    app_version: str
    online_status: str
    brightness: Optional[int] = None
    volume: Optional[int] = None
    last_active: float  # 最后活动时间戳
